
package com.zcp.notepad;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class ChecklistEditText extends EditText {

    public ChecklistEditText(Context context) {
        super(context);
    }

    public ChecklistEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ChecklistEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // 选择标志
    private boolean checkflag;

    private CheckBox checkbox;

    // 用于删除事件，判断在按删除键之前是否内容已经为空
    private boolean isEmptyBefore;

    private NoteEditorActivity activity;

    public void setCheckbox(CheckBox checkbox) {
        this.checkbox = checkbox;
        this.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkflag = isChecked;
                if (isChecked) {
                    setTextColor(Color.GRAY);
                    if (activity != null) {
                        activity.moveChildViewToLast(ChecklistEditText.this);
                    }
                } else {
                    setTextColor(Color.BLACK);
                    activity.restoreChildView(ChecklistEditText.this);
                }
            }
        });
    }

    public boolean getCheckflag() {
        return checkflag;
    }

    public void setCheckflag(boolean checkflag) {
        this.checkflag = checkflag;
        checkbox.setChecked(checkflag);
    }

    public void setActivity(NoteEditorActivity activity) {
        this.activity = activity;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (!focused) {
            if (TextUtils.isEmpty(getText().toString().trim())) {
                checkbox.setVisibility(View.INVISIBLE);
            } else {
                checkbox.setVisibility(View.VISIBLE);
            }
        } else {
            checkbox.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int before, int after) {

        if (activity == null)
            return;
        String content = text.toString();
        if (text.length() == 0 && activity.getChildViewIndex(this) == 0) {
            activity.disableDoneButton();
        } else {
            activity.enableDoneButton();
        }
        if (checkbox != null && TextUtils.isEmpty(content.trim()) && before == 0) {
            checkbox.setVisibility(View.INVISIBLE);
        }

        int splitIndex;
        if ((splitIndex = content.indexOf("\n")) == -1)
            return;
        int index = activity.getChildViewIndex(this);
        String[] list = content.split("\n");
        int length = list.length;

        content = content.substring(0, splitIndex).trim();
        setText(content);
        if (checkbox == null)
            return;
        else {
            if (TextUtils.isEmpty(content.trim())) {
                checkbox.setVisibility(View.INVISIBLE);
            }
        }
        if (length <= 1) {
            activity.insertChecklistItem("", index + 1).requestFocus();

        } else {
            for (int i = 1; i < length; i++) {
                activity.insertChecklistItem(list[i], index + 1).requestFocus();
            }

        }

        super.onTextChanged(text, start, before, after);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL) {
            if (TextUtils.isEmpty(getText())) {
                isEmptyBefore = true;
            } else {
                isEmptyBefore = false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL) {
            if (isEmptyBefore && activity.getChecklistCount() > 1) {
                ChecklistEditText text = activity.removeChildView(this);
                text.requestFocus();
                text.setSelection(text.length());
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    /**
     * 在文字下方划线效果
     */
    // @Override
    // protected void onDraw(Canvas canvas) {
    // Rect r = mRect;
    // Paint paint = mPaint;
    // int lineHeight = getLineHeight();
    // int count = Math.max(getLineCount(), getHeight() / lineHeight);
    // int lineY = getLineBounds(0, r);
    // // 由于getLineHeight与字体高度有误差，所以当写了一行文字时将改变的高度作为标准高度
    // if (getLineCount() > 1) {
    // lineHeight = getLineBounds(1, r) - lineY;
    // }
    // for (int i = 0; i < count; i++) {
    // canvas.drawLine(r.left, lineY + 2, r.right, lineY + 2, paint);
    // lineY += lineHeight;
    // }
    // super.onDraw(canvas);
    // }
}
